<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\SalaryRule;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class SalaryRuleController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        SalaryRule::where('company_id',$company_id)->update(['status'=>2,'mtime'=>time()]);
        $data = $request->data;
        $name = $data['name'];
        foreach ($data['list'] as $k=>$v) {
            if ( $v['id']) {
                $insured = SalaryRule::findOrFail($v['id']);
                $insured->status = 1;
                $insured->mtime = time();
            } else {

                $insured = new SalaryRule();
                $insured->ctime = time();
            }
            $insured->company_id = $company_id;
            $insured->name = $name;
            $insured->calculation_method = $v['calculation_method'];
            $insured->money = $v['money'];
            $insured->no_buckle = $v['no_buckle'];
            $insured->type = $v['type'];
            $insured->uid = $uid;

            $insured->save();
        }


        return showError(Error::ERR_OK,['id'=>$insured->id]);

    }

    public function show(Request $request) {
        $insured = SalaryRule::findOrFail($request->id);
        return showError(Error::ERR_OK,['insured'=>$insured]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];
        $name = $request->input('name');
        if (!empty($name))
        {
            $where['name'] = $name;
        }

        $where['company_id'] = $company_id;
        $where['status'] = 1;

        $list = SalaryRule::getList($where);

        return showError(Error::ERR_OK,['list'=>$list]);
    }

    public function delete(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        SalaryRule::where('company_id',$company_id)->update(['status'=>2,'mtime'=>time()]);
        return showError(Error::ERR_OK);
    }


}
